<?php
    class Daftar_con extends CI_Controller{
        private $idterm;
        private $term;
        private $tahun;
        private $nama;
        function __construct() {
            parent::__construct();
            $query=  $this->db->get_where('term',array('STATUS'=>'SEDANG'));
            $row=$query->row();
            $this->idterm=$row->ID_TERM;
            $this->term=$row->NO_TERM;
            $this->tahun=$row->TAHUN;
        }
        function index(){
            //menampilkan halaman login
            $this->form_login();
        }
        function form_login(){
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/login_view',$display);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function validate_login(){
            $usr=$this->input->post('username');
            $password=$this->input->post('password');
            $strquery="SELECT *
                FROM (
                user
                NATURAL JOIN role
                )
                NATURAL JOIN daftar_role
                WHERE USERNAME='".$usr."'
                AND PASSWORD='".md5($password)."'
                AND NAMA_ROLE='FO'    
                ";
            $query=$this->db->query($strquery);
            if($query->num_rows()>0){
                $result=$query->row();
                $this->nama=$result->NAMA;
                $data= array(
                    'Loggedin'=>TRUE,
                    'Nama'=>$result->NAMA,
                    'ID'=>$result->ID
                );
                $this->session->set_userdata($data);
                redirect(base_url().'pendaftaran/daftar_con/home');
            }else{
                redirect(base_url().'pendaftaran');
            }
        }
        function logout(){
            $data= array(
                    'Loggedin'=>FALSE,
                    'Nama'=>'',
                    'ID'=>''
                );
            $this->session->unset_userdata($data);
            redirect(base_url().'pendaftaran');
        }
        function home(){
            if($this->session->userdata('Loggedin')==FALSE){ 
                redirect(base_url().'pendaftaran');
            }
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$this->idterm;
            $query=$this->db->query($strquery);
            $display['total']=$query->row()->jumlah;
            
            $display['hasil']=$this->db->query('select * from pelatihan natural join kelas')->result_array();
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/home_view',$display);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function list_pendaftar($kode,$angkatan){
            if($this->session->userdata('Loggedin')==FALSE){ 
                redirect(base_url().'pendaftaran');
            }
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$this->idterm;
            $query=$this->db->query($strquery);
            $display['total']=$query->row()->jumlah;
            
            $hasil=$this->db->query('select * from pelatihan natural join kelas where ANGKATAN='.$angkatan.' and KODE_PELATIHAN="'.$kode.'"')->row_array();
//            $display['nama']=$hasil['NAMA_PELATIHAN'];
            $display['kode']=$kode;
            $display['angkatan']=$angkatan;
            $display['kuota']=$hasil['KUOTA'];
            $display['harga']=$hasil['HARGA'];
            
            $jum=$this->db->query('select count(ID_USER) as jum from peserta_kelas where ANGKATAN='.$angkatan.' and KODE_PELATIHAN="'.$kode.'"')->row();
            $display['jumlah']=$jum->jum;
            
            $display['pendaftar']=$this->db->query('select * from user inner join peserta_kelas on user.ID=peserta_kelas.ID_USER where ANGKATAN='.$angkatan.' and KODE_PELATIHAN="'.$kode.'"')->result_array();
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/pendaftar_view',$display);
            $this->load->view('pendaftaran/daftar_footer_view');

        }
        function setting(){
            
        }
        function daftar(){
            if($this->session->userdata('Loggedin')==FALSE){ 
                redirect(base_url().'pendaftaran');
            }
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/pilih_daftar_view',$display);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function daftar_new(){
            if($this->session->userdata('Loggedin')==FALSE){ 
                redirect(base_url().'pendaftaran');
            }
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            $query=$this->db->get('kelas');
            $display['pelatihan']['']='';
            foreach($query->result() as $entry){
                $kode=$entry->KODE_PELATIHAN.'-'.$entry->ANGKATAN;
                $display['pelatihan'][$kode]=$kode;
            }
            $query=$this->db->get('diskon');
            $i=0;
            foreach($query->result() as $entry){
                $display['diskon'][$i]['no']=$entry->ID_DISKON;
                $display['diskon'][$i]['nama']=$entry->NAMA_DISKON;
                $i++;
            }
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/form_new_view',$display);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function daftar_new_proses(){
            if($this->session->userdata('Loggedin')==FALSE){ 
                redirect(base_url().'pendaftaran');
            }
            
            
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
                        
            $nama=$this->input->post('nama');
            $nohp=$this->input->post('nohp');
            $gol=$this->input->post('gol');
            $kelas=  $this->input->post('kelas');
            $username=str_ireplace(' ','.',  strtolower($nama));
            $password=md5($username);
            $data=array(
                'NAMA'=>$nama,
                'NO_TELP'=>$nohp,
                'GOL'=>$gol,
                'USERNAME'=>$username,
                'PASSWORD'=>$password
            );
            
            $this->db->insert('user',$data);
            $this->db->select_max('ID');
            $id=$this->db->get('user')->row()->ID;
            
            $besar_diskon=0;
            
            $no=$this->input->post('nocic');
            if($no!=''){
                $query=$this->db->get_where('peserta',array(
                    'NO_CIC_CARD'=>$no,'STATUS'=>"TRUE"
                ));
                if($query->num_rows()==1){
                    $besar_diskon=20;
                    $update=array('STATUS'=>'FALSE');
                    $this->db->where('NO_CIC_CARD', $no);
                    $this->db->update('peserta', $update);
                }else{
                    //diskon fail
                    $data['pesan']='No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="javascript:back()">Kembali</a>';
                    $this->load->view('pendaftaran/daftar_header_view');
                    $this->load->view('pendaftaran/menu_atas',$display);
                    $this->load->view('pendaftaran/pesan_kesalahan',$data);
                    $this->load->view('pendaftaran/daftar_footer_view');
                    
                }
            }else{
                //ngecek diskon yg dicentang
                $diskon=$this->input->post('diskon');
                if(!empty($diskon)){
                    for($i=0;$i<count($diskon);$i++){
                        $hasil=$this->db->get_where('diskon',array('ID_DISKON'=>$diskon[$i]))->row();
                        if(strcasecmp($hasil->NAMA_DISKON, 'diskon golongan')==0){
                            switch($gol){
                                case 1:
                                    $besar_diskon+=10;
                                    break;
                                case 2:
                                    $besar_diskon+=10;
                                    break;
                                case 3:
                                    $besar_diskon+=5;
                                    break;
                                case 4:
                                    $besar_diskon+=0;
                                    break;
                                case 5:
                                    $besar_diskon+=100;
                                    break;
                            }
                        }else{
                            $besar_diskon+=$hasil->BESAR;
                        }
                    }
                }
            }
            if($query->num_rows()==1){
                $pelatihan=explode('-', $kelas);
                $kode=$pelatihan[0];
                $angkatan=$pelatihan[1];
                $result=$this->db->get_where('kelas',array(
                    'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row();
                $harga=$result->HARGA;
                if($besar_diskon>100)
                    $besar_diskon=100;
                $total_bayar=(100-$besar_diskon)/100*$harga;
                $data=array(
                    'ID_USER'=>$id,
                    'ANGKATAN'=>$angkatan,
                    'KODE_PELATIHAN'=>$kode,
                    'TOTAL_BAYAR'=>$total_bayar,
                    'TGL_DAFTAR'=>date('Y-m-d'),
                    'ID_TERM'=>$this->idterm,
                    'ID_FO'=>  $this->session->userdata('ID')
                );
                $this->db->insert('peserta_kelas',$data);

                $gen_nocic=$this->convert_no($id).'-'.$this->cut_year($this->tahun).$gol;
                $data=array('NO_CIC_CARD'=>$gen_nocic,'ID'=>$id,'STATUS_CIC_CARD'=>'FALSE');
                $this->db->insert('peserta ',$data);
                $data['id']=$id;
                $data['nama']=$nama;
                $data['nohp']=$nohp;
                $data['no_cic']=$gen_nocic;
                $data['kelas']=$kelas;
                $data['pelatihan'] = $kode;
                $data['angkatan'] = $angkatan;
                $data['biaya']=$harga;
                $data['diskon']=$besar_diskon;
                $data['bayar']=$total_bayar;

                $result=$this->db->query('select count(JUMLAH) as total from pembayaran where ID='.$id)->row();
                $data['sudah_bayar']=$result->total;




                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas',$display);
                $this->load->view('pendaftaran/biaya_view',$data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }
        }
        function display_biaya($id,$kode,$angkatan){
            
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            
            $user=$this->db->get_where('user',array('ID'=>$id))->row_array();
            $data['id']=$id;
            $data['nama']=$user['NAMA'];
            $data['nohp']=$user['NO_TELP'];
            $peserta=$this->db->get_where('peserta',array('ID'=>$id))->row_array();
            $data['no_cic']=$peserta['NO_CIC_CARD'];
            
            $peserta_kelas=$this->db->get_where('peserta_kelas',array('ID_USER'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row_array();
            
            $data['bayar']=$peserta_kelas['TOTAL_BAYAR'];
            
            $pelatihan=$peserta_kelas['KODE_PELATIHAN'];
            $angkatan=$peserta_kelas['ANGKATAN'];
            $data['pelatihan']=$pelatihan;
            $data['angkatan']=$angkatan;
            $data['kelas']=$pelatihan.'-'.$angkatan;
            
            $kelas=$this->db->get_where('kelas',array('KODE_PELATIHAN'=>$pelatihan,'ANGKATAN'=>$angkatan))->row_array();
            $data['biaya']=$kelas['HARGA'];
            
            $result=$this->db->query('select sum(JUMLAH) as total from pembayaran where ID='.$id.' and KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan)->row();
            $data['sudah_bayar']=$result->total;
            
            $potongan = $data['biaya']-$data['bayar'];
            
            $data['diskon']=$potongan*100/$data['biaya'];
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/biaya_view',$data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function bayar($id,$kode,$angkatan){
            
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            
            $user=$this->db->get_where('user',array('ID'=>$id))->row_array();
            $data['id']=$id;
            $data['nama']=$user['NAMA'];
            $data['nohp']=$user['NO_TELP'];
            $peserta=$this->db->get_where('peserta',array('ID'=>$id))->row_array();
            $data['no_cic']=$peserta['NO_CIC_CARD'];
            $data['kode']=$kode;
            $data['angkatan']=$angkatan;
            $peserta_kelas=$this->db->get_where('peserta_kelas',array('ID_USER'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row_array();
            
            $data['bayar']=$peserta_kelas['TOTAL_BAYAR'];
            
            $pelatihan=$peserta_kelas['KODE_PELATIHAN'];
            $angkatan=$peserta_kelas['ANGKATAN'];
            
            $data['kelas']=$pelatihan.'-'.$angkatan;
            
            $kelas=$this->db->get_where('kelas',array('KODE_PELATIHAN'=>$pelatihan,'ANGKATAN'=>$angkatan))->row_array();
            $data['biaya']=$kelas['HARGA'];
            
            $result=$this->db->query('select sum(JUMLAH) as total from pembayaran where ID='.$id.' and KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan)->row();
            $data['sudah_bayar']=$result->total;
            
            $potongan = $data['biaya']-$data['bayar'];
            
            $data['diskon']=$potongan*100/$data['biaya'];
            
            $this->db->order_by('TGL_BAYAR','asc');
            $data['history']=  $this->db->get_where('pembayaran',array('ID'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->result_array();
            
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/bayar_view',$data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function input_bayar($id,$kode,$angkatan){
            
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            
            $besar_bayar=$this->input->post('besar');
            $tgl=date('Y-m-d H:i:s');
            
            $data=array(
                'ID'=>$id,
                'TGL_BAYAR'=>$tgl,
                'JUMLAH'=>$besar_bayar,
                'ID_FO'=>$id_fo,
                'KODE_PELATIHAN'=>$kode,
                'ANGKATAN'=>$angkatan
            );
            $this->db->insert('pembayaran',$data);
            
            $user=$this->db->get_where('user',array('ID'=>$id))->row_array();
            $data['id']=$id;
            $data['nama']=$user['NAMA'];
            $data['nohp']=$user['NO_TELP'];
            $peserta=$this->db->get_where('peserta',array('ID'=>$id))->row_array();
            $data['no_cic']=$peserta['NO_CIC_CARD'];
            
            $data['tgl']=$tgl;
            $data['besar']=$besar_bayar;
            
            $peserta_kelas=$this->db->get_where('peserta_kelas',array('ID_USER'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row_array();
            
            $pelatihan=$peserta_kelas['KODE_PELATIHAN'];
            $angkatan=$peserta_kelas['ANGKATAN'];
            $data['kode']=$pelatihan;
            $data['angkatan']=$angkatan;
            $data['kelas']=$pelatihan.'-'.$angkatan;
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/struk_view',$data);
            $this->load->view('pendaftaran/daftar_footer_view');
            
        }
        function convert_no($no){
            $strno=''.$no;
            for($i=0;$i<5-strlen($strno);$i++){
                $strno='0'.$strno;
            }
            return $strno;
        }
        function cut_year($year){
            $stryear=''.$year;
            return substr($stryear, 2);
        }
        function daftar_old(){
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/cari_nocic');
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function cari_user(){
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            $nocic=$this->input->post('nocic');
            $strquery='select * from peserta inner join user on peserta.ID=user.ID where peserta.NO_CIC_CARD="'.$nocic.'"';
            $query=$this->db->query($strquery);
            //echo $nocic.' '.$strquery;
            if($query->num_rows==1){
                $hasil=$query->row_array();
                $data['id']=$hasil['ID'];
                $data['nama']=$hasil['NAMA'];
                $data['nocic']=$hasil['NO_CIC_CARD'];
                $data['pesan']='No CIC anda sudah tidak dapat digunakan <br/> <a href="'.base_url().'pendaftaran/daftar_con/daftar">Daftar baru</a>';
                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas',$display);
                $this->load->view('pendaftaran/hasilcari_view',$data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }else{
                $data['pesan']='No CIC anda sudah tidak dapat digunakan <br/> <a href="'.base_url().'pendaftaran/daftar_con/daftar">Daftar baru</a>';
                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas',$display);
                $this->load->view('pendaftaran/pesan_kesalahan',$data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }
        }
        function form_daftar_old($id){
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            
            $query=$this->db->get('kelas');
            $data['pelatihan']['']='';
            foreach($query->result() as $entry){
                $kode=$entry->KODE_PELATIHAN.'-'.$entry->ANGKATAN;
                $data['pelatihan'][$kode]=$kode;
            }
            $query=$this->db->get('diskon');
            $i=0;
            foreach($query->result() as $entry){
                $data['diskon'][$i]['no']=$entry->ID_DISKON;
                $data['diskon'][$i]['nama']=$entry->NAMA_DISKON;
                $i++;
            }
            
            $data['user']=  $this->db->query('select * from user where ID='.$id)->row_array();
            $data['peserta']=$this->db->query('select * from peserta where ID='.$id)->row_array();
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/form_old_view',$data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
        function daftar_old_proses(){
            
            $nama=$this->input->post('nama');
            $nohp=$this->input->post('nohp');
            $gol=$this->input->post('gol');
            $kelas=  $this->input->post('kelas');
            
            
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            
            $besar_diskon=0;
            
            $no=$this->input->post('nocic');
            
            if($no!=''){
                $query=$this->db->get_where('peserta',array(
                    'NO_CIC_CARD'=>$no,'STATUS'=>'TRUE'
                ));
                if($query->num_rows()==1){
//                    echo $besar_diskon;
//            die($besar_diskon);
                    $besar_diskon=25;
                    $update=array('STATUS'=>'FALSE');
                    $this->db->where('NO_CIC_CARD', $no);
                    $this->db->update('peserta', $update);
                }else{
                    //diskon fail
                    $data['pesan']='No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="javascript:back()">Kembali</a>';
                    $this->load->view('pendaftaran/daftar_header_view');
                    $this->load->view('pendaftaran/menu_atas',$display);
                    $this->load->view('pendaftaran/pesan_kesalahan',$data);
                    $this->load->view('pendaftaran/daftar_footer_view');
                }
            }else{
                //ngecek diskon yg dicentang
                $diskon=$this->input->post('diskon');
                if(!empty($diskon)){
                    for($i=0;$i<count($diskon);$i++){
                        $hasil=$this->db->get_where('diskon',array('ID_DISKON'=>$diskon[$i]))->row();
                        if(strcasecmp($hasil->NAMA_DISKON, 'diskon golongan')==0){
                            switch($gol){
                                case 1:
                                    $besar_diskon+=15;
                                    break;
                                case 2:
                                    $besar_diskon+=15;
                                    break;
                                case 3:
                                    $besar_diskon+=15;
                                    break;
                                case 4:
                                    $besar_diskon+=15;
                                    break;
                                case 5:
                                    $besar_diskon+=15;
                                    break;
                            }
                        }else{
                            $besar_diskon+=$hasil->BESAR;
                        }
                    }
                }
            }
            
            $pelatihan=explode('-', $kelas);
            $kode=$pelatihan[0];
            $angkatan=$pelatihan[1];
            $result=$this->db->get_where('kelas',array(
                'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row();
            $harga=$result->HARGA;
            $total_bayar=(100-$besar_diskon)/100*$harga;
            $id=$this->input->post('id');
            $data=array(
                'ID_USER'=>$id,
                'ANGKATAN'=>$angkatan,
                'KODE_PELATIHAN'=>$kode,
                'TOTAL_BAYAR'=>$total_bayar,
                'TGL_DAFTAR'=>date('Y-m-d'),
                'ID_TERM'=>$this->idterm,
                'ID_FO'=>  $this->session->userdata('ID')
            );
            $this->db->insert('peserta_kelas',$data);
            redirect(base_url().'pendaftaran/daftar_con/display_biaya/'.$id.'/'.$kode.'/'.$angkatan);
        }
        function history($id,$kode,$angkatan){
            $display['nama']=  $this->session->userdata('Nama');
            $display['noterm']=$this->term;
            $display['thnterm']=$this->tahun;
            $id_fo=$this->session->userdata('ID');
            $strquery="SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=".$id_fo;
            $query=$this->db->query($strquery);
            $display['jumlah']=$query->row()->jumlah;
            
            $user=$this->db->get_where('user',array('ID'=>$id))->row_array();
            $data['id']=$id;
            $data['nama']=$user['NAMA'];
            $data['nohp']=$user['NO_TELP'];
            $peserta=$this->db->get_where('peserta',array('ID'=>$id))->row_array();
            $data['no_cic']=$peserta['NO_CIC_CARD'];
            $data['kode']=$kode;
            $data['angkatan']=$angkatan;
            $peserta_kelas=$this->db->get_where('peserta_kelas',array('ID_USER'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->row_array();
            
            $data['bayar']=$peserta_kelas['TOTAL_BAYAR'];
            
            $pelatihan=$peserta_kelas['KODE_PELATIHAN'];
            $angkatan=$peserta_kelas['ANGKATAN'];
            
            $data['kelas']=$pelatihan.'-'.$angkatan;
            
            $kelas=$this->db->get_where('kelas',array('KODE_PELATIHAN'=>$pelatihan,'ANGKATAN'=>$angkatan))->row_array();
            $data['biaya']=$kelas['HARGA'];
            
            $result=$this->db->query('select sum(JUMLAH) as total from pembayaran where ID='.$id.' and KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan)->row();
            $data['sudah_bayar']=$result->total;
            
            $potongan = $data['biaya']-$data['bayar'];
            
            $data['diskon']=$potongan*100/$data['biaya'];
            
            $this->db->order_by('TGL_BAYAR','asc');
            $data['history']=  $this->db->get_where('pembayaran',array('ID'=>$id,'KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan))->result_array();
            
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display);
            $this->load->view('pendaftaran/history_view',$data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }

        function konversi_hari($nomor)
        {
            switch ($nomor)
            {
                case 1: return 'Senin';
                case 2: return 'Selasa';
                case 3: return 'Rabu';
                case 4: return 'Kamis';
                case 5: return 'Jumat';
                case 6: return 'Sabtu';
                case 7: return 'Minggu';
            }
        }

        function division($bil1,$bil2)
        {
            $temp=($bil1-1)/$bil2;
            $temp2=($bil1-1)%$bil2;
            return ($temp-($temp2/$bil2));
        }

        function isgenap($nomor)
        {
            return (($nomor%2)==0);
        }

        function konversi_kode_waktu($nomor1,$nomor2)
        {
            $hasil = 15+$this->division($nomor1,2);
            $hasil .= '.';
            if(!$this->isgenap($nomor1))
                $hasil .= '00';
            else
                $hasil .= '30';
            $hasil .= '-';
            $hasil .= 15+$this->division($nomor2,2)+1;
            $hasil .= '.';
            if($this->isgenap($nomor2))
                $hasil .= '00';
            else
                $hasil .= '30';            
            return $hasil;
        }

        function konversi_rupiah($angka)
        {
            $panjang=strlen($angka);
            $hasil = '';
            if($panjang <3)
                return $angka;
            for($i=3;$i<$panjang;$i+=3)
            {
                $hasil = substr($angka,($panjang-$i),3).$hasil;
                if($i!=$panjang)
                $hasil = '.'.$hasil;
            }
            if($panjang>($i-3))
            $hasil = substr($angka,0,($panjang-$i)).$hasil;
            return $hasil;
        }

        function cetak_kuitansi($id='',$kode='',$angkatan=''){
            $data['kode'] = $kode;
            $data['angkatan'] = $angkatan;
            $result = $this->db->get_where('user',array('ID'=>$id));
            $array = $result->result_array();
            $data['nama_peserta']=$array[0]['NAMA'];
            $data['tanggal']= date('d m Y H.i');
            $data['nama_petugas']= $this->session->userdata('Nama');
            $data['no_hp_peserta']=$array[0]['NO_TELP'];
            $result = $this->db->get_where('peserta',array('ID'=>$id));
            $array = $result->result_array();
            $data['no_cic_peserta']=$array[0]['NO_CIC_CARD'];
            $result = $this->db->get_where('pelatihan',array('KODE_PELATIHAN'=>$kode));
            $array = $result->result_array();
            $data['nama_pelatihan']=$array[0]['NAMA_PELATIHAN'];
            $result = $this->db->get_where('kelas',array('KODE_PELATIHAN'=>$kode,'ANGKATAN'=>$angkatan));
            $array = $result->result_array();
            $data['jam_total']=$array[0]['JAM_TOTAL'];
            $data['pertemuan']=$array[0]['JAM_TOTAL']/$array[0]['JAM_PER_SESI'];
            $data['term']= $array[0]['TERM'];
            $data['harga'] = $array[0]['HARGA'];
            $data['harga2'] = $this->konversi_rupiah($data['harga'].'');
            $data['sesi_per_minggu'] = $array[0]['SESI_PER_MINGGU'];
            $result = $this->db->get_where('term',array('NO_TERM'=>$data['term']));
            $array = $result->result_array();
            $data['tahun_term']=$array[0]['TAHUN'];
            $result = $this->db->query('SELECT ID_RUANGAN, HARI
                                        FROM  jadwal_pelatihan
                                        WHERE KODE_PELATIHAN="'.$kode.'"
                                        AND ANGKATAN = '.$angkatan.'
                                        GROUP BY  HARI
                                        LIMIT 0 , 30');
            $array = $result->result_array();
            $hari= array();
            $jam = array();
            for($i=0;$i<count($array);$i++)
            {
                $hari[$i]=$this->konversi_hari($array[$i]['HARI']);
                $result = $this->db->query('SELECT MIN(KODE_WAKTU) AS min,MAX(KODE_WAKTU) as max FROM `jadwal_pelatihan` WHERE HARI ='.$array[$i]['HARI'].' AND KODE_PELATIHAN="'.$kode.'"
                                        AND ANGKATAN = '.$angkatan);
                $array2 = $result->result_array();
                $jam[$i] = $this->konversi_kode_waktu($array2[0]['min'],$array2[0]['max']);
            }
            $data['hari'] = $hari;
            $data['jam'] = $jam;
            
            $result = $this->db->query('SELECT *
                                        FROM  pembayaran WHERE ID='. $id .' AND
                                        KODE_PELATIHAN = "'.$kode.'" AND
                                        ANGKATAN = '.$angkatan.'
                                        ORDER BY  TGL_BAYAR DESC ');
            $array = $result->result_array();
            $data['jumlah_pembayaran'] = $array[0]['JUMLAH'];
            $data['tanggal_pembayaran'] = substr($array[0]['TGL_BAYAR'],0,10);
            $result = $this->db->get_where('peserta_kelas',array('ID_USER'=>$id,'ANGKATAN'=>$angkatan,'KODE_PELATIHAN'=>$kode));
            $array = $result->result_array();
            $data['total_bayar']=$array[0]['TOTAL_BAYAR'];
            $result = $this->db->query('SELECT count(*)AS hitung,SUM(JUMLAH) AS jumlah
                                        FROM  pembayaran WHERE ID='. $id .' AND
                                        KODE_PELATIHAN = "'.$kode.'" AND
                                        ANGKATAN = '.$angkatan.'
                                        ORDER BY  TGL_BAYAR DESC ');
            $array = $result->result_array();
            $data['pembayaran_ke'] = $array[0]['hitung'];
            $data['sisa_pembayaran'] = $data['total_bayar']-$array[0]['jumlah'];
            $data['diskon'] =  ($data['harga']-$data['total_bayar'])/$data['harga']*100;
            

            $this->load->view('pendaftaran/print',$data);
        }
    }
?>
